<?php
<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="卡号/姓名/手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option label="正常" :value="1" />
          <el-option label="禁用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="卡号" prop="card_no" />
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="手机号" prop="mobile" />
      <el-table-column label="额度" prop="limit" />
      <el-table-column label="余额" prop="balance" />
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
            {{ scope.row.status === 1 ? '正常' : '禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button 
            type="text" 
            :class="{ 'danger': scope.row.status === 1 }"
            @click="handleStatusChange(scope.row)"
          >
            {{ scope.row.status === 1 ? '禁用' : '启用' }}
          </el-button>
          <el-button type="text" @click="handleRecharge(scope.row)">充值</el-button>
          <el-button type="text" @click="handleSubCards(scope.row)">管理子卡</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />

    <!-- 表单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="额度" prop="limit">
          <el-input v-model="form.limit" placeholder="请输入额度" type="number" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addCard, updateCard, updateStatus } from '@/api/black-card'
import Pagination from '@/components/Pagination'

export default {
  name: 'BlackCardList',
  components: { Pagination },
  data() {
    return {
      loading: false,
      total: 0,
      list: [],
      open: false,
      title: '',
      queryParams: {
        page: 1,
        limit: 10,
        keyword: undefined,
        status: undefined
      },
      form: {
        card_id: undefined,
        name: undefined,
        mobile: undefined,
        limit: undefined
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        limit: [{ required: true, message: '请输入额度', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      try {
        const { data } = await getList(this.queryParams)
        this.list = data.list
        this.total = data.total
      } catch (error) {
        console.error(error)
      }
      this.loading = false
    },
    handleQuery() {
      this.queryParams.page = 1
      this.getList()
    },
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    handleAdd() {
      this.title = '新增黑卡'
      this.form = {
        card_id: undefined,
        name: undefined,
        mobile: undefined,
        limit: undefined
      }
      this.open = true
    },
    handleEdit(row) {
      this.title = '编辑黑卡'
      this.form = { ...row }
      this.open = true
    },
    async handleStatusChange(row) {
      try {
        await this.$confirm(
          `确认要${row.status === 1 ? '禁用' : '启用'}该黑卡吗？`,
          '提示',
          {
            type: 'warning'
          }
        )
        await updateStatus({
          card_id: row.card_id,
          status: row.status === 1 ? 0 : 1
        })
        this.$message.success('操作成功')
        this.getList()
      } catch (error) {
        console.error(error)
      }
    },
    handleRecharge(row) {
      this.$router.push(`/black-card/recharge?card_id=${row.card_id}`)
    },
    handleSubCards(row) {
      this.$router.push(`/black-card/sub?parent_card_id=${row.card_id}`)
    },
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            if (this.form.card_id) {
              await updateCard(this.form)
            } else {
              await addCard(this.form)
            }
            this.$message.success('操作成功')
            this.open = false
            this.getList()
          } catch (error) {
            console.error(error)
          }
        }
      })
    },
    cancel() {
      this.open = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.danger {
  color: #f56c6c;
}
</style> 